﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using PSE.ProcessManager.Entity.Enums;
using System.Xml;

namespace PSE.ProcessManager.Entity
{
    [Serializable]
    public class ProcessEntity
    {
        public ProcessEntity()
        {
            this.ProcessId = -1;
        }

        [System.Xml.Serialization.XmlIgnore()]
        public int ProcessId
        {
            get;
            set;
        }

        public int? Id { get; set; }
        public int InteligenceModuleId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public InstanceContext InstanceContext { get; set; }
        public string Class { get; set; }
        public string Assembly { get; set; }
        //public FailureActions FirstFailureRecovery { get; set; }
        //public FailureActions SecondFailureRecovery { get; set; }
        //public FailureActions SubsequentFailuresRecovery { get; set; }
        public FailureActions FailureRecovery { get; set; }
        public StartUpType InitializationType { get; set; }

        [System.Xml.Serialization.XmlIgnore()]
        public List<MessageEntity> Messages
        {
            get;
            set;
        }

        ///// <summary>
        ///// Indica a quantidade de falhas ocorridas no processo.
        ///// </summary>
        //[System.Xml.Serialization.XmlIgnore()]
        //public int ProcessFailureState
        //{
        //    get;
        //    set;
        //}

        public FailureActions ResolveFailureAction(string failureAction)
        {
            switch (failureAction.ToUpper())
            {
                case "N":
                    return FailureActions.TakeNoAction;
                case "R":
                    return FailureActions.RestartProcess;
                default:
                    throw new Exception(string.Format("Invalid failure action {0}.", failureAction));
            }
        }

        public InstanceContext ResolveInstanceContext(string instanceContext)
        {
            switch (instanceContext.ToUpper())
            {
                case "S":
                    return InstanceContext.Single;
                case "M":
                    return InstanceContext.Multiple;
                default:
                    throw new Exception(string.Format("Invalid instance context {0}.", instanceContext));
            }
        }

        public StartUpType ResolveStartUpType(string initializationType)
        {
            switch (initializationType.ToUpper())
            {
                case "A":
                    return StartUpType.Automatic;
                case "M":
                    return StartUpType.Manual;
                default:
                    throw new Exception(string.Format("Invalid instance context {0}.", initializationType));
            }
        }
    }
}
